package upload

import (
	"common/pb"
	"common/report"
	"euplink/upload/upClient"
	"time"
)

func ConvertUploadResultToStoreInfo(uploadResults []upClient.PieceUploadResult) []*pb.PieceStoreInfo {
	var piecesStoreInfos []*pb.PieceStoreInfo = nil
	for _, result := range uploadResults {
		if result.Err != nil {
			continue
		}
		piecesStoreInfos = append(piecesStoreInfos, &pb.PieceStoreInfo{
			Index:         int32(result.Num),
			NodeId:        result.NodeId.Bytes(),
			Hash:          result.HashValue,
			PlainSize:     0,
			EncryptedSize: int64(result.PieceSize),
		})
	}
	return piecesStoreInfos
}

func convertUploadResultStat(clientId string, tryCount int, time time.Duration,
	success bool, results []upClient.PieceUploadResult) *report.Stat {

	arr := make([]report.DeviceStat, 0)
	for _, r := range results {
		errString := ""
		if r.Err != nil {
			errString = r.Err.Error()
		}
		arr = append(arr, report.DeviceStat{
			Id:    r.DevId,
			Speed: 0,
			Done:  r.Err == nil,
			Error: errString,
		})
	}

	return &report.Stat{
		Action:      "upload",
		ClientId:    clientId,
		Success:     success,
		Try:         tryCount,
		UseTime:     float32(time.Seconds()),
		DeviceStats: arr,
	}

}
